import { deepMerge } from '@/utils/index';
import { FormFiled } from './props';

interface MergeConfigItem extends FormFiled {
  detail?: Partial<FormFiled>;
  edit?: Partial<FormFiled>;
}

type MergeConfigType = 'detail' | 'edit';

export const getEditOrDetailConfig = (
  mergeConfigList: MergeConfigItem[],
  type: MergeConfigType
): FormFiled[] => {
  const arr: MergeConfigItem[] = mergeConfigList;
  const list = arr.map((t) => {
    const { detail = {}, edit = {} } = t;
    const item = type === 'detail' ? detail : edit;

    deepMerge(t, item);
    delete t.edit;
    delete t.detail;

    if (type === 'detail') {
      if (!t.component) {
        t.component = 'DetailDesc';
      }
    }

    return t;
  });

  return list;
};
